home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Network / Batch Copy / PowerBatch V1.2 < prev   
Encoding:
Text File  |  1999-03-04  |  16.4 KB  |  632 lines  |  [TEXT/ToyS]

  1. -- User setable properties
  2. property kasAllowAlerts : true -- Set to false to stop any alerts from appearing
  3. property kasServerAlerts : true -- Set to false to stop warnings about missed servers
  4. property kasBatchCopyList : ":Batch Copy List" -- The list of files that we should copy
  5. property kasBatchDest : "::" -- The destination of the copies
  6. property kasOverwrite : false -- Overwrite existing?
  7. property kasResume : true -- Resume partial copies?
  8. property kasJustDoIt : false -- Disconnect wrong ARA connections without asking?
  9.  
  10. -- Internal/User setable globals
  11. property kasTest : false -- Just testing the script?
  12.  
  13. -- Internal/User setable globals
  14. property gasLinkSet : false -- Has the friend been asked for? - set to true to never ask
  15. property gasLinkNeed : false
  16. property kasLinkUserDft : "CasaVision" -- Defaults for linking access
  17. property kasLinkPassDft : ""
  18.  
  19. property gasServSet : false
  20. property gasServNeed : false
  21. property kasServUserDft : "Synapse" -- Defaults for server access
  22. property kasServPassDft : ""
  23. property kasServNTDft : ""
  24.  
  25. -- Internal globals
  26. global gasLinkUser -- Our friend on all machines
  27. global gasLinkPass -- Our friend's password on all machines
  28. global gasServUser -- Our friend on all machines
  29. global gasServPass -- Our friend's password on all machines
  30. global gasServNT -- Volume password
  31.  
  32. -- Internal globals
  33. property kasBatchType : "RemoteBatch"
  34. property gasOurAlias : "" -- The alias we own
  35. property gasOurStatus : "" -- Empty, "RemoteLauncher" or "RemoteFolder"
  36. property gasOurServer : "" -- Set to server of gasOurAlias
  37. property gasOurZone : "" -- Set to zone of gasOurAlias
  38. property gasOurName : "" -- Name of original alias
  39. property gasOurVol : "" -- Name of volume alias resides on
  40. property gasOurType : "" -- If we are a RemoteLauncher, this is our file type
  41. property gasCopyLoc : {0, 0} -- The location of the copy window
  42.  
  43. -- Remote access stuff
  44. property kraConnected : "connected" -- ARA osax strings
  45. property kraProtocol : "ARAP"
  46. property gasRemote : false -- set internally to true if volume was a RemoteAccess volume
  47. property gasRemoteCfg : "" -- Set internally to ARA config needed for this access
  48. property gasRemoteServer : "" -- Set internally to check if we are already connected to the correct server
  49. property gasRemoteAlias : false --Set internally to rebuild ARA with alias instead of osax
  50. property gasRemoteNum : "" -- Set internally to the telephone number of the remote machine
  51. property gasRemoteUsr : "" -- Set internally to the login user for the remote machine
  52.  
  53.  
  54. on run
  55.     if (kasTest) then
  56.         set gasOurAlias to ((item 1 of (list disks)) & ":") as alias
  57.         RunRemoteBatch()
  58.     else
  59.         if (gasOurStatus is "") then
  60.             ShowInitAlert()
  61.         else if (gasOurStatus is kasBatchType) then
  62.             RunRemoteBatch()
  63.         else
  64.             beep
  65.         end if
  66.     end if
  67. end run
  68.  
  69.  
  70. on open fsObjs
  71.     if (gasOurStatus is "") then
  72.         if the number of items in fsObjs is not 1 then
  73.             ShowInitAlert()
  74.         else
  75.             SetScriptAlias(item 1 of fsObjs)
  76.         end if
  77.     else if (gasOurStatus is kasBatchType) then
  78.         set wasMounted to IsVolMounted(gasOurVol)
  79.         if (RunRemoteBatch()) then
  80.             repeat with fsObj in fsObjs
  81.                 try
  82.                     set gasCopyLoc to ¬
  83.                         (AkuaCopy fsObj located at gasCopyLoc with resuming given «class Dest»:gasOurAlias)
  84.                 on error err
  85.                     ShowAlert("An error (" & (err as string) & ") occured copying “" & (fsObj as string) & "”. The network may have been interrupted or a disk error may have occured")
  86.                 end try
  87.             end repeat
  88.             if (not wasMounted) then ¬
  89.                 PutAwayVol(gasOurVol)
  90.         end if
  91.     end if
  92. end open
  93.  
  94.  
  95. on RunRemoteBatch()
  96.     -- Get the user/password
  97.     GetFriend(false)
  98.     
  99.     if (not gasServSet) then return
  100.     
  101.     set mounted to false
  102.     
  103.     -- Mount the remote volume
  104.     if (not gasRemote or not gasRemoteAlias) then
  105.         try
  106.             «event ÅkuNMSVm» gasOurVol ¬
  107.                 given «class MSrv»:gasOurServer ¬
  108.                 , «class TZon»:gasOurZone ¬
  109.                 , «class USER»:gasServUser ¬
  110.                 , «class PASS»:gasServPass ¬
  111.                 , «class VPwd»:gasServNT
  112.             set mounted to true
  113.         on error
  114.             if (CouldBeSharing(gasOurZone, gasOurServer)) then
  115.                 try
  116.                     «event ÅkuNMSVm» gasOurVol ¬
  117.                         given «class MSrv»:gasOurServer ¬
  118.                         , «class TZon»:gasOurZone ¬
  119.                         , «class USER»:gasServUser ¬
  120.                         , «class PASS»:gasServPass ¬
  121.                         , «class VPwd»:gasServNT
  122.                 on error
  123.                     if (kasServerAlerts) then
  124.                         ShowMountAlert()
  125.                     else
  126.                         beep
  127.                     end if
  128.                     set mounted to false
  129.                 end try
  130.             else
  131.                 if (kasServerAlerts) then
  132.                     ShowMountAlert()
  133.                 else
  134.                     beep
  135.                 end if
  136.                 set mounted to false
  137.             end if
  138.         end try
  139.     end if
  140.     
  141.     if (mounted) then
  142.         try
  143.             -- Mount the remote volume
  144.             set sourceFolder to resolve server alias gasOurAlias ¬
  145.                 as user name gasServUser with password gasServPass ¬
  146.                 with volume password gasServNT
  147.             set mounted to true
  148.         on error
  149.             set mounted to false
  150.             if (kasServerAlerts) then
  151.                 if (gasRemote) then
  152.                     ShowRemoteAlert()
  153.                 else
  154.                     ShowMountAlert()
  155.                 end if
  156.             else
  157.                 beep
  158.             end if
  159.         end try
  160.         
  161.         if (mounted) then
  162.             -- Our destination folder
  163.             set destFolder to PathToAlias(kasBatchDest)
  164.             
  165.             -- Read in our list
  166.             set myBatchList to PathToAlias(kasBatchCopyList)
  167.             set myBatchText to read data from the data fork of myBatchList
  168.             
  169.             -- Kill comments
  170.             set myBatchText to (collect lines of myBatchText that match "&#:*")
  171.             RunRemoteCopy(sourceFolder, destFolder, myBatchText)
  172.         end if
  173.     end if
  174.     
  175.     return mounted
  176. end RunRemoteBatch
  177.  
  178.  
  179. property kasPgLoc : {0, 0}
  180.  
  181. on RunRemoteCopy(src, dst, itemList)
  182.     set srcItems to list folder src
  183.     set srcPath to src as string
  184.     
  185.     set pg to display progress titled ¬
  186.         "Batch Copy" subtitled ("Total Items: " & the number of items of srcItems) ¬
  187.         located at kasPgLoc
  188.     
  189.     repeat with x in srcItems
  190.         display progress pg labeled x
  191.         
  192.         if (the kind of entry in src named x) is a folder then
  193.             RunRemoteCopy((srcPath & x) as alias, dst, itemList)
  194.         else if (collect lines of itemList that match x) is not "" then
  195.             try
  196.                 set gasCopyLoc to ¬
  197.                     (AkuaCopy ((srcPath & x) as alias) resuming kasResume located at gasCopyLoc given «class Dest»:dst, «class Over»:kasOverwrite)
  198.             on error
  199.                 beep
  200.             end try
  201.         end if
  202.     end repeat
  203.     
  204.     set pgLoc to screen location of (display progress pg with disposal)
  205.     if (pgLoc is not kasPgLoc) then set kasPgLoc to pgLoc
  206. end RunRemoteCopy
  207.  
  208.  
  209. on SetScriptAlias(aliasObj)
  210.     -- My Path
  211.     set myPath to (path to me)
  212.     
  213.     -- Grab an alias
  214.     set gasOurAlias to aliasObj
  215.     set aInfo to alias info from gasOurAlias
  216.     set fInfo to basic info for gasOurAlias
  217.     set xInfo to extended info for myPath
  218.     
  219.     -- Save the info we need
  220.     set gasOurServer to alias server of aInfo
  221.     set gasOurZone to alias zone of aInfo
  222.     set gasOurVol to alias volume of aInfo
  223.     set gasOurName to original name of aInfo
  224.     set gasOurType to system type of fInfo
  225.     
  226.     -- Set our parent's name to dropped name
  227.     set newName to "Batch " & (catalog name of fInfo)
  228.     if (length of newName > 30) then ¬
  229.         set newName to the text from character 1 ¬
  230.             to (30 - (length of newName)) of newName
  231.     try
  232.         collate (parent spec of xInfo) renaming it to newName
  233.     on error
  234.         beep
  235.     end try
  236.     
  237.     -- What will we be
  238.     set gasServNeed to true
  239.     set gasWasRemote to IsRemoteAccessed()
  240.     set gasOurStatus to kasBatchType
  241.     
  242.     -- Get the user/password
  243.     GetFriend(false)
  244.     
  245.     -- Check ARA connection
  246.     set gasRemote to IsRemoteAccessed()
  247.     if (gasRemote) then WantRemoteAccess()
  248.     
  249.     -- Set our icon to dropped icon
  250.     set myPath to (path to me)
  251.     set ourIcon to (the icon for gasOurAlias)
  252.     set the icon of myPath to ourIcon
  253.     
  254.     try
  255.         tell application "Finder" to update myPath -- Double dose?
  256.     on error
  257.         beep
  258.     end try
  259. end SetScriptAlias
  260.  
  261.  
  262. on GetFriend(override)
  263.     -- Build ARA connection?
  264.     if (gasRemote and not gasRemoteAlias) then ¬
  265.         if not BuildRemoteAccess() then ¬
  266.             return
  267.     
  268.     -- Get linking (and server) user?
  269.     if (gasLinkNeed) then
  270.         GetOneFriend(true, override)
  271.         if (gasRemote and gasRemoteAlias) then ¬
  272.             GetOneFriend(false, override)
  273.     end if
  274.     
  275.     -- Get server user?
  276.     if (gasServNeed) then GetOneFriend(false, override)
  277. end GetFriend
  278.  
  279.  
  280. on GetOneFriend(isLink, override)
  281.     if (isLink) then
  282.         set userMode to "linking"
  283.         if (gasLinkSet) then
  284.             set defUser to gasLinkUser
  285.             set defPass to gasLinkPass
  286.         else
  287.             set defUser to kasLinkUserDft
  288.             set defPass to kasLinkPassDft
  289.         end if
  290.         set defNT to kasServNTDft
  291.         set passButtons to {"Cancel", "OK"}
  292.         set passButton to 2
  293.     else
  294.         set userMode to "sharing"
  295.         if (gasServSet) then
  296.             set defUser to gasServUser
  297.             set defPass to gasServPass
  298.             set defNT to gasServNT
  299.         else
  300.             set defUser to kasServUserDft
  301.             set defPass to kasServPassDft
  302.             set defNT to kasServNTDft
  303.         end if
  304.         set passButtons to {"Cancel", "NT Password…", "OK"}
  305.         set passButton to 3
  306.     end if
  307.     
  308.     set usrPwd to KeyChainLookUp(gasOurZone, gasOurServer, isLink)
  309.     
  310.     if (override or usrPwd is {}) then
  311.         set chosen to display dialog ¬
  312.             "Enter the friendly " & userMode & ¬
  313.             " user's name for the server \"" & gasOurServer ¬
  314.             & "\"…" default answer defUser ¬
  315.             default button 2 with icon note
  316.         
  317.         if (the button returned of chosen is "OK") then
  318.             set defUser to the text returned of chosen
  319.         else
  320.             return
  321.         end if
  322.         
  323.         set chosen to display dialog ¬
  324.             "Enter the friendly " & userMode & ¬
  325.             " user's password…" buttons passButtons ¬
  326.             default answer defPass default button passButton with icon note
  327.         
  328.         if (the button returned of chosen is not "Cancel") then
  329.             set defPass to the text returned of chosen
  330.         else
  331.             return
  332.         end if
  333.         
  334.         if (the button returned of chosen is "NT Password…") then
  335.             set chosen to display dialog ¬
  336.                 "Enter the volume password for Windoze NT…" default answer ¬
  337.                 defNT default button 2 with icon note
  338.             
  339.             if (the button returned of chosen is not "Cancel") then
  340.                 set gasServNT to the text returned of chosen
  341.             else
  342.                 return
  343.             end if
  344.         end if
  345.         
  346.         -- Save encrypted user/pass for future access
  347.         KeyChainSave(gasOurZone, gasOurServer, isLink, defUser, defPass, "")
  348.     else
  349.         set defUser to item 1 of usrPwd
  350.         set defPass to item 2 of usrPwd
  351.         set defNT to item 3 of usrPwd
  352.     end if
  353.     
  354.     if (isLink) then
  355.         set gasLinkSet to true
  356.         set gasLinkUser to defUser
  357.         set gasLinkPass to defPass
  358.     else
  359.         set gasServSet to true
  360.         set gasServUser to defUser
  361.         set gasServPass to defPass
  362.         set gasServNT to defNT
  363.     end if
  364. end GetOneFriend
  365.  
  366.  
  367. on PathToAlias(pathStr)
  368.     if (character 1 of pathStr is ":") then
  369.         set daddy to parent spec of (extended info for (path to me))
  370.         set daddyStr to the text from character 1 to -2 of (daddy as string)
  371.         set pathStr to daddyStr & pathStr
  372.     end if
  373.     
  374.     return pathStr as alias
  375. end PathToAlias
  376.  
  377.  
  378. on PutAwayVol(volName)
  379.     -- Volume
  380.     try
  381.         set vAlias to (vol alias of (the volume info of volume named volName))
  382.         tell application "Finder"
  383.             update vAlias
  384.             pause for 2 with seconds timing -- Give some time to the Finder?
  385.             put away item vAlias
  386.         end tell
  387.     on error
  388.         beep
  389.     end try
  390. end PutAwayVol
  391.  
  392.  
  393. on IsVolMounted(volName)
  394.     -- Volume
  395.     try
  396.         set x to (volName & ":") as alias
  397.         return true
  398.     on error
  399.         return false
  400.     end try
  401. end IsVolMounted
  402.  
  403.  
  404. property sasShareLoc : {0, 0}
  405.  
  406. on CouldBeSharing(servZone, servName)
  407.     GetOneFriend(true, false) -- Get linking user
  408.     
  409.     talk as user gasLinkUser ¬
  410.         with password gasLinkPass ¬
  411.         on server servName ¬
  412.         in AppleTalk zone servZone
  413.     
  414.     try
  415.         set isOn to («class fshr» ¬
  416.             of application kasFinder ¬
  417.             of machine servName ¬
  418.             of zone servZone)
  419.         
  420.         if (not isOn) then
  421.             set «class fshr» of application kasFinder ¬
  422.                 of machine servName ¬
  423.                 of zone servZone to true
  424.             
  425.             set inf to (display info titled ¬
  426.                 "Wait for Sharing" message ¬
  427.                 (servZone & ":" & servName) ¬
  428.                     located at sasShareLoc)
  429.             
  430.             set waitSecs to 333
  431.             
  432.             repeat while not («class fshr» of application kasFinder ¬
  433.                 of machine servName ¬
  434.                 of zone servZone)
  435.                 display info inf message ¬
  436.                     ("Wait another " & waitSecs & " seconds") at line 2
  437.                 pause for 2 with seconds timing
  438.                 set waitSecs to waitSecs - 2
  439.                 if (waitSecs < 1) then exit repeat
  440.             end repeat
  441.             
  442.             if («class fshr» of application kasFinder ¬
  443.                 of machine servName ¬
  444.                 of zone servZone) then
  445.                 display info inf message ¬
  446.                     "It is up!" at line 2
  447.             else
  448.                 display info inf message ¬
  449.                     "Can't get it up!" at line 2
  450.                 beep
  451.             end if
  452.             
  453.             set sasShareLoc to screen location of ¬
  454.                 (display info inf with disposal)
  455.             
  456.             return («class fshr» of application ¬
  457.                 kasFinder of machine servName of zone servZone)
  458.         end if
  459.     on error errStr
  460.         if (errStr contains "-911") then
  461.             GetOneFriend(true, true)
  462.             return CouldBeSharing()
  463.         end if
  464.     end try
  465.     
  466.     return false
  467. end CouldBeSharing
  468.  
  469.  
  470. on ShowAccessAlert()
  471.     ShowServerAlert("The remote access connection for the item on " & gasOurServer ¬
  472.         & " in zone " & gasOurZone & " failed to be made.")
  473. end ShowAccessAlert
  474.  
  475.  
  476. on ShowRemoteAlert()
  477.     ShowServerAlert("Couldn't find remote item on " & gasOurServer ¬
  478.         & " in zone " & gasOurZone & ¬
  479.         " due to ARA connection problems or changes in the remote zone or on the remote volume.")
  480. end ShowRemoteAlert
  481.  
  482.  
  483. on ShowMountAlert()
  484.     ShowServerAlert("Couldn't mount volume " & gasOurVol & ¬
  485.         " from " & gasOurServer ¬
  486.         & " in zone " & gasOurZone)
  487. end ShowMountAlert
  488.  
  489.  
  490. on ShowConnectAlert(appName)
  491.     ShowServerAlert("Couldn't connect to " & appName & " on " & ¬
  492.         gasOurServer & " in zone " & gasOurZone)
  493. end ShowConnectAlert
  494.  
  495.  
  496. on ShowServerAlert(msg)
  497.     set choice to ¬
  498.         display dialog msg buttons {"Reenter Password", "OK"} ¬
  499.             default button 2 with icon stop
  500.     
  501.     if (button returned of choice is not "OK") then GetFriend(true)
  502. end ShowServerAlert
  503.  
  504.  
  505. on ShowInitAlert()
  506.     ShowAlert("This script must first be activated by dropping exactly one item on it." & ¬
  507.         return & return & "Please see the information file that accompanied it.")
  508. end ShowInitAlert
  509.  
  510.  
  511. on ShowOpenAlert()
  512.     ShowAlert("This script is currently in Launcher mode and can thus not process any files dropped on it." & ¬
  513.         return & return & "To reconfigure the script, open it with ScriptEditor and save it.")
  514. end ShowOpenAlert
  515.  
  516.  
  517. on ShowAlert(msgStr)
  518.     if (kasAllowAlerts) then ¬
  519.         display dialog msgStr buttons {"Damn!"} ¬
  520.             default button 1 with icon stop
  521. end ShowAlert
  522.  
  523.  
  524. on ShowChoice(msgStr)
  525.     return (ShowChoices(msgStr, {"No", "Yes"}) is "Yes")
  526. end ShowChoice
  527.  
  528.  
  529. on ShowChoices(msgStr, choices)
  530.     set choice to ¬
  531.         display dialog msgStr buttons choices ¬
  532.             default button (number of items of choices) ¬
  533.             with icon stop
  534.     return (button returned of choice)
  535. end ShowChoices
  536.  
  537.  
  538. property kasKeyChainPassword : "PowerScript" -- Encrypt stored data with this
  539. property kasPrefsFileName : "PowerScript Prefs" -- File name in <Preferences>
  540.  
  541. on KeyChainLookUp(zoneName, serverName, isLinking)
  542.     set prefOwner to "πSRV"
  543.     if (isLinking) then set prefOwner to "πLNK"
  544.     
  545.     try
  546.         set myKeyData to load preference named (zoneName & ":" & serverName) ¬
  547.             of type prefOwner in file named kasPrefsFileName
  548.     on error
  549.         return {}
  550.     end try
  551.     
  552.     return (encrypt the data myKeyData with password kasKeyChainPassword)
  553. end KeyChainLookUp
  554.  
  555.  
  556. on KeyChainSave(zoneName, serverName, isLinking, usr, pwd, ntPwd)
  557.     set myKey to encrypt the data {usr, pwd, ntPwd} with password kasKeyChainPassword
  558.     
  559.     set prefOwner to "πSRV"
  560.     if (isLinking) then set prefOwner to "πLNK"
  561.     
  562.     save preference myKey named (zoneName & ":" & serverName) ¬
  563.         of type prefOwner ¬
  564.         in file named kasPrefsFileName
  565. end KeyChainSave
  566.  
  567.  
  568. on IsRemoteAccessed()
  569.     try
  570.         set araStats to (RA status)
  571.     on error
  572.         return false
  573.     end try
  574.     
  575.     return ((state of araStats) is kraConnected) ¬
  576.         and ((protocol of araStats) is kraProtocol)
  577. end IsRemoteAccessed
  578.  
  579.  
  580. on WantRemoteAccess()
  581.     set gasRemoteAlias to true
  582.     
  583.     if kasJustDoIt or ¬
  584.         (ShowChoice("PowerScript noticed that an ARA connection is active." & return & return & ¬
  585.             "Do you wish the connection to be built before searching for the item?" & return & return & ¬
  586.             "(If you answer No, the ARA connection may be made by resolving the alias.)")) then
  587.         
  588.         -- Get the ARA config for the alias
  589.         set configs to RA configurations whose numbers are gasRemoteNum ¬
  590.             whose users are gasRemoteUsr
  591.         
  592.         if (the number of items in configs) > 0 then
  593.             set gasRemoteCfg to item 1 of (item 1 of configs) -- The name of the config to use
  594.             set gasRemoteServer to server name of (RA status)
  595.             set gasRemoteAlias to false
  596.         end if
  597.     end if
  598. end WantRemoteAccess
  599.  
  600.  
  601. on BuildRemoteAccess()
  602.     set araIsOn to IsRemoteAccessed()
  603.     
  604.     if (araIsOn) then
  605.         -- Connected to correct server?
  606.         if (server name of (RA status)) is gasRemoteServer then
  607.             return true
  608.         else
  609.             RA disconnect
  610.             set araIsOn to IsRemoteAccessed()
  611.         end if
  612.     end if
  613.     
  614.     if (not araIsOn) then
  615.         -- Save current config, set it to ours, connect, set it back
  616.         set saveConfig to RA configuration gasRemoteCfg
  617.         
  618.         try
  619.             RA connect
  620.         on error
  621.             beep
  622.         end try
  623.         
  624.         RA configuration saveConfig
  625.         
  626.         if (IsRemoteAccessed()) then return true
  627.     end if
  628.     
  629.     ShowAccessAlert()
  630.     return false
  631. end BuildRemoteAccess
  632.